<?php

namespace app\controllers;

use app\common\utils\QyWechat;
use Yii;
use app\models\GkyQyhDepartment;
use app\models\GkyQyhDepartmentSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * GkyQyhDepartmentController implements the CRUD actions for GkyQyhDepartment model.
 */
class GkyQyhDepartmentController extends Controller
{
    public $layout='main_1';
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all GkyQyhDepartment models.
     * @return mixed
     */
    public function actionIndex($isUpdata = false)
    {
        if($isUpdata){
            GkyQyhDepartment::updataByAll();
        }

        $searchModel = new GkyQyhDepartmentSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single GkyQyhDepartment model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new GkyQyhDepartment model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new GkyQyhDepartment();

        if ($model->load(Yii::$app->request->post())) {

            //获取当前企业号
            $qyh = $model->qyh;
            $model->id = $qyh->id.$model->dno;

            if($model->save()){
                $options = array(
                    'token'=>'gky',   //填写应用接口的Token
                    'encodingaeskey'=>$qyh->aeskey,//填写加密用的EncodingAESKey
                    'appid'=>$qyh->corpid,  //填写高级调用功能的appid
                    'appsecret'=>$qyh->secret, //填写高级调用功能的密钥
                );
                $weObj = new QyWechat($options);
                $strlen = strlen($model->qyh_id.'');
                $parentid = substr($model->parentid.'',$strlen);
                $data = array(
                    "id" => $model->dno,               //(必须)部门id
                    "name" =>  $model->name,   //(非必须)部门名称
                    "parentid" =>  $parentid.'',         //(非必须)父亲部门id。根部门id为1
                    "order" =>  $model->order,            //(非必须)在父部门中的次序。从1开始，数字越大排序越靠后
                );
                $result = $weObj->createDepartment($data);
                return $this->redirect(['view', 'id' => $model->id]);
            }else{
                return $this->render('create', [
                    'model' => $model,
                ]);
            }

        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing GkyQyhDepartment model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {

            //获取当前企业号
            $qyh = $model->qyh;

            $options = array(
                'token'=>'gky',   //填写应用接口的Token
                'encodingaeskey'=>$qyh->aeskey,//填写加密用的EncodingAESKey
                'appid'=>$qyh->corpid,  //填写高级调用功能的appid
                'appsecret'=>$qyh->secret, //填写高级调用功能的密钥
            );
            $weObj = new QyWechat($options);
            $strlen = strlen($model->qyh_id.'');
            $parentid = substr($model->parentid.'',$strlen);
            $data = array(
              "id" => $model->dno,               //(必须)部门id
              "name" =>  $model->name,   //(非必须)部门名称
              "parentid" =>  $parentid.'',         //(非必须)父亲部门id。根部门id为1
              "order" =>  $model->order,            //(非必须)在父部门中的次序。从1开始，数字越大排序越靠后
            );
            $result = $weObj->updateDepartment($data);

            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing GkyQyhDepartment model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $model = $this->findModel($id);
        $model->delete();

        //获取当前企业号
        $qyh = $model->qyh;

        $options = array(
            'token'=>'gky',   //填写应用接口的Token
            'encodingaeskey'=>$qyh->aeskey,//填写加密用的EncodingAESKey
            'appid'=>$qyh->corpid,  //填写高级调用功能的appid
            'appsecret'=>$qyh->secret, //填写高级调用功能的密钥
        );
        $weObj = new QyWechat($options);
//        $strlen = strlen($model->qyh_id.'');
//        $parentid = substr($model->parentid.'',$strlen);
//        $data = array(
//            "id" => $model->dno,               //(必须)部门id
//            "name" =>  $model->name,   //(非必须)部门名称
//            "parentid" =>  $parentid.'',         //(非必须)父亲部门id。根部门id为1
//            "order" =>  $model->order,            //(非必须)在父部门中的次序。从1开始，数字越大排序越靠后
//        );
        $result = $weObj->deleteDepartment($model->dno);

        return $this->redirect(['index']);
    }


    /**
     * Finds the GkyQyhDepartment model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return GkyQyhDepartment the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = GkyQyhDepartment::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
